<template>
  <!-- 头部工具栏区域组件，主要管理路由回退前进，检索，登录信息 -->
  <div class="header-bar">
    <div class="logo-wrap pointer" @click="toHomePage">
      <img style="height: 40px;width: 50px;margin-right: 100px;"
        class="logo-title"
        
        src="../../assets/img/云听.png"
        alt="小雨云听"
        title="Rain"
      />
      <!-- https://ebui-cdn.bj.bcebos.com/yiyan-logo.png-->
    </div>
    <!-- 移动端菜单按钮 -->
    <div class="menu-btn pointer" @click="openMenu">
      <span :class="{ span_active: showMenuInPhone }"></span>
    </div>
    <!-- 历史按钮 -->
    <div class="btn-history">
      <button @click="goTo(-1)" class="btn-circle">
        <i class="iconfont icon-arrow-left-bold"></i>
      </button>
      <button @click="goTo(1)" class="btn-circle">
        <i class="iconfont icon-arrow-right"></i>
      </button>
    </div>
    
    <div class="search-box">
      <!-- 自定义桌面 -->
      <div class="skin" @click="toSkin">
        <i class="iconfont icon-clothes"></i>
      </div>
      <!-- 搜索框 -->
      <div class="search-input">
        <el-input
          style="width: 200px"
          placeholder="搜索"
          v-model="keywords"
          ref="inputRef"
          clearable
          @focus="getSearchHistory"
          @blur="showInfoTip = false"
          prefix-icon="el-icon-search"
        >
        </el-input>
        <transition name="el-fade-in">
          <!-- 热搜及搜索建议 -->
          <div class="search-info_tip" v-show="showInfoTip">
            <div>
              <div class="search-suggest">
                <div class="search-btn-wrap">
                  <button class="no-btn">
                    搜“{{ keywords }}”相关的内容<i
                      class="el-icon-arrow-right"
                    ></i>
                  </button>
                </div>
                <SuggestList v-if="filterList.length > 0">
                  <template #title>
                    <i class="iconfont icon-yinle font-16"></i
                    ><span class="mleft-10">单曲</span>
                  </template>
                  <template #item>
                    <div
                      v-for="s in filterList"
                      :key="s.id"
                      class="item pointer text-hidden"
                      @click="playMusic(s.id)"
                    >
                      {{ s.title }} - {{ s.artist }}
                    </div>
                  </template>
                </SuggestList>
              </div>
            </div>
          </div>
        </transition>
      </div>
      <div class="avatar-wrap mleft-12 pointer" @click="loginView">
        <el-avatar
          :size="30"
          icon="el-icon-user-solid"
          :src="avatarUrl"
        ></el-avatar>
      </div>
      <!-- <div
        class="login-info mleft-10 font-12 text-hidden"
        :class="{ pointer: isLogin }"
        @click="doLogout"
      >
        {{ name }}
      </div> -->
      <el-dropdown @command="handleCommand">
        <span class="el-dropdown-link login-info mleft-10 font-12 text-hidden">
          {{ name }}<i class="el-icon-arrow-down el-icon--right"></i>
        </span>
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item command="0" v-if="isLogin">退出</el-dropdown-item>
          <el-dropdown-item command="1" v-if="!isLogin">登录</el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
    </div>
  </div>
</template>

<script>
import { mapState } from "vuex";
import { getAllMusicList } from "@/api/api_music.js";
import { getUserDetail } from "@/api/api_user";
import SuggestList from "@/components/list/SuggestList";
export default {
  components: { SuggestList },
  data() {
    return {
      keywords: "",
      showInfoTip: false, //展示搜索建议
      hotList: [], //热搜列表
      historySearchList: [], //历史搜索
      suggestInfo: [], //建议
      showMenuInPhone: false,
      avatarUrl: require("../../assets/img/avatar.jpeg"),
    };
  },
  watch: {
    profile: {
      handler(newVal, oldVal) {
        if (this.isLogin) {
          if (newVal !== oldVal) {//之前是这样的数据，还有.data,s说明里面还是可以给点东西的！
            // const { photoPath } = newVal?.data;
            const { photoPath } = newVal;
            if (photoPath) this.avatarUrl = photoPath;
            else {
              const photoPath = JSON.parse(
                localStorage.getItem("profile")
              ).data.photoPath;

              this.avatarUrl = photoPath
                ? photoPath
                : require("../../assets/img/avatar.jpeg");
            }
          } else {
            const photoPath = JSON.parse(
              localStorage.getItem("profile")
            ).photoPath;
            this.avatarUrl = photoPath;
          }
        } else {
          this.avatarUrl = null;
        }
      },
      //不行，还是得将它打开，实现登录上来头像立马更换。
      immediate: true,//因为一些问题，我关闭了这个立即监听！
    },
    isLogin: {
      async handler(newVal) {
        if (newVal) {
          const token = localStorage.getItem("token");
          const res = await getUserDetail(token);
          if (res.status !== 200)
            this.$message({ type: "error", message: "出错了~" });
          if (res.data.data) {
            // this.$store.commit("setProfile", res.data.data);
            this.$store.commit("setProfile", res.data);//这个问题还是数据给的不是很正确
          }
        }
      },
      immediate: true,
    },
  },
  computed: {
    /* 登录相关信息 */
    ...mapState(["isLogin", "isPhone", "token", "profile"]),
    name() {
      return this.isLogin ? localStorage.getItem("user") : "未登录";
    },
    /* 搜索建议相关状态 */
    showMusic() {
      return this.suggestInfo.find(
        (item) => item.title.indexOf(this.keywords) !== -1
      );
    },
    filterList() {
      const res = this.suggestInfo.filter(
        (item) => item.title.indexOf(this.keywords) !== -1
      );
      return res;
    },
  },
  methods: {
    handleCommand(command) {
      if (command === "0") {
        this.doLogout();
      }
      if (command === "1") {
        this.$router.push("/login");
      }
    },
    doLogout() {
      this.$store.dispatch("logout");
    },
    getSearchHistory() {
      this.showInfoTip = true;
      this.getSuggest();
    },
    async getSuggest() {
      const res = await getAllMusicList(99990);//直接随机返回数据
      if (res.code === 200) {
        this.suggestInfo = res.songList;
      }
    },
    /* 网页logo点击回调 */
    toHomePage() {
      if (this.$route.path != "/personalrecom")
        this.$router.push("/personalrecom");
    },
    /* 前进后退 */
    goTo(step) {
      this.$router.go(step);
    },
    /* 登录页面 */
    loginView() {
      if (!this.isLogin) this.$router.push("/login");
      else {
        if (this.$route.path === "/userdetail/") return;
        this.$router.push("/userdetail/");
      }
    },
    toPlayListDetail(id) {
      if (typeof id === "number") {
        this.$router.push("/playlistdetail/" + id);
      }
    },
    /* 处理搜索建议中单曲数据格式。并播放 */
    async playMusic(id) {
      const index = this.suggestInfo.findIndex((item) => item.id === id);
      if (typeof id !== "number") return;
      this.$store.commit("setMusicList", this.suggestInfo);
      this.$store.commit("setCurrenMusicId", id);
      this.$store.commit("setPlayState", true);
      this.$store.commit("setCurrenIndex", index);
    },
    openMenu() {
      if (!this.showMenuInPhone)
        document.querySelector(".aside").style.left = 0 + "px";
      else document.querySelector(".aside").style.left = -200 + "px";
      this.showMenuInPhone = !this.showMenuInPhone;
    },
    /* 编程式路由导航到skin皮肤组件 */
    toSkin(){
      if(this.$route.path=="/skin")return;
      this.$router.push("/skin");      
    }
  },
};
</script>

<style lang="less" scoped>
.header-bar {
  height: 100%;
  display: flex;
  align-items: center;
  color: #ffffff;
}

.logo-wrap {
  height: 60px;
  line-height: 60px;

  .icon-logView {
    font-size: 48px;
  }
  .logo-title {
    margin: 15px 0 0 15px;
    width: 65%;
  }
}

.menu-btn {
  display: none;
}

.btn-history {
  margin-left: 20px;
  display: flex;

  /* 圆按钮 */
  .btn-circle {
    display: flex;
    height: 26px;
    width: 26px;
    outline: 0;
    border: 0;
    background-color: #0062af;
    color: #ffffff;
    justify-content: center;
    align-items: center;
    border-radius: 50%;
    margin-right: 10px;
    cursor: pointer;
  }
}
.search-box {
  position: absolute;
  right: 7px;
  display: flex;
  flex-direction: row;
  justify-content: flex-end;
  align-items: center;
}
.skin{
  margin-left: 20px;
  position: relative;
  cursor: pointer;
  &:hover i{
    color: rgb(101, 245, 122);
  }
  &:active i{
    transform: scale(0.8);
  }

  i{
    font-size: 26px;
  }
}
/* 搜索框容器 */
.search-input {
  margin-left: 10px;
  position: relative;
  

  /* 搜索建议等的弹出层 */
  .search-info_tip {
    position: absolute;
    top: 40px;
    left: 0;
    width: 340px;
    min-height: 340px;
    max-height: 420px;
    transition: all 0.5s;
    overflow-y: auto;
    border-radius: 8px;
    box-shadow: 0 1px 4px #dddddd;
    background-color: #fff;
    z-index: 100;
    color: #000;
  }
}

/* 搜索建议顶部按钮区域 */
.search-btn-wrap {
  height: 30px;
  line-height: 30px;
}

.login-info {
  max-width: 4rem;
  font-size: 14px;
  color: #fff;
}

@media screen and (max-width: 768px) {
  .menu-btn {
    /* 移动端菜单按钮 */
    display: inline-block;
    width: 24px;
    height: 24px;
    position: relative;

    span {
      position: absolute;
      display: inline-block;
      width: 20px;
      height: 2px;
      top: 2px;
      left: 4px;
      background-color: #fff;
      transition: none;

      &::before,
      &::after {
        position: absolute;
        content: "";
        display: inline-block;
        width: 20px;
        height: 2px;
        background-color: #fff;
      }

      &::before {
        top: 8px;
        left: 0;
      }

      &::after {
        left: 0;
        top: 16px;
      }
    }

    .span_active {
      transition: transform 0.5s;
      transform: rotate(-45deg);
      top: 10px;

      &::before {
        opacity: 0;
      }

      &::after {
        transform: rotate(-90deg);
        top: 0px;
      }
    }
  }

  .btn-history {
    margin-left: 10px;

    .btn-circle {
      &:nth-child(2) {
        display: none;
      }
    }
  }

  .logo-wrap {
    display: none;
  }

  .search-input {
    margin: 0;
  }
}

@media screen and (max-width: 415px) {
  .search-input {
    .search-info_tip {
      left: -60px;
    }
  }
}
</style>
